﻿import com.dinmax.factorium.*

class FactoriumApp {
	
	public static var OBJETIVO:Number = 0;
	public static var MECANICA:Number = 1;
	public static var INFO:Number = 2;
	public static var MAPA:Number = 3;
	
	private static var app:FactoriumApp;
	private static var LAST_LEVEL:Number=0;
	private static var MASK_LEVEL:Number=100;
	private static var FILES:Array = ["presentacion.swf", "paisaje.swf", "beeky.swf", "shadow.swf", "cartel.swf", "mecanica.swf", "control.swf"];
	private static var N_FILE:Number = 0;
	private var mclExternal:MovieClipLoader;
	private var oExternalListener:Object;
		
	private var mcShadow:FactoriumShadow;
	private var mcPaisaje:FactoriumPaisaje;
	private var mcBeeKy:FactoriumBeeKy;
	private var mcControl:FactoriumControl;
	private var activeContent:FactoriumContent;
	private var objetivoContent:FactoriumContent;
	private var mecanicaContent:FactoriumContent;
		
	public static function newApp():Void {
		FactoriumApp.app = new FactoriumApp();	
		return;
	};
	public static function getApp():FactoriumApp {
		return app;
	};
	
	public static function quitApp():Void {
		fscommand("quit");
		return;
	};
	
	private static function getLastLevel():Number {
		return FactoriumApp.LAST_LEVEL;
	};
	private static function getNextLevel():Number {
		FactoriumApp.LAST_LEVEL++;
		return FactoriumApp.getLastLevel();
	};
	private static function getNextFile():String {
		FactoriumApp.N_FILE++;
		return FactoriumApp.FILES[N_FILE-1];
	};
	
	function FactoriumApp() {
		fscommand("fullscreen", "true");
		fscommand("trapallkeys", "true");
		Stage.scaleMode = "noScale";
		Stage.showMenu = false;
		
		this.initLoader();
		this.loadMask();
		return;
	};
	
	public function setShadow(s:FactoriumShadow):Void {
		this.mcShadow = s;
		return;
	};
	public function setPaisaje(p:FactoriumPaisaje):Void {
		this.mcPaisaje = p;
		return;
	};
	public function setBeeKy(b:FactoriumBeeKy):Void {
		this.mcBeeKy = b;
		return;
	};
	public function setControl(c:FactoriumControl):Void {
		this.mcControl = c;
		return;
	};
	public function getControl():FactoriumControl {
		return this.mcControl;
	};

	public function setActiveContent(a:FactoriumContent):Void {
		this.activeContent = a;
		return;
	};
		
	private function loadMask():Void {
		this.mclExternal.loadClip("mask.swf", FactoriumApp.MASK_LEVEL);
		return;
	};
	
	private function initLoader():Void {
		this.oExternalListener = new Object();
		this.oExternalListener.onLoadError = function(target:MovieClip, code:String, httpStatus:Number):Void {
			//trace("FactoriumApp.as | loadError");
			FactoriumApp.getApp().loadNextFile();
			return;
		};
		this.oExternalListener.onLoadStart = function():Void {
			//trace("FactoriumApp.as | loadStart");
			return;
		};
		this.oExternalListener.onLoadProgress = function(target:MovieClip, loaded:Number, total:Number):Void {
			trace("cargando: "+target+"( "+Math.floor(loaded/1024)+" de "+Math.floor(total/1024)+" kb)");
			return;
		};
		this.oExternalListener.onLoadComplete = function():Void {
			//trace("FactoriumApp.as | loadComplete");
			return;
		};
		this.oExternalListener.onLoadInit = function(clip:MovieClip):Void {
			if(clip.mcClick){
				clip.mcClick.onPress = function():Void {
					this._parent.removeSounds();
					FactoriumApp.getApp().loadNextFile();
					//this._parent.gotoAndStop(1);
					this._parent.unloadMovie();
					return;
				}
				
			} else {
				FactoriumApp.getApp().loadNextFile();
			};
			return;
		};
		
		this.mclExternal = new MovieClipLoader();
		this.mclExternal.addListener(oExternalListener);
		
		return;
	};
	
	public function loadFile(path:String, target:MovieClip):Void {
		if(!target){
			mclExternal.loadClip(path, FactoriumApp.getNextLevel());
		} else {
			mclExternal.loadClip(path, target);
		};
		return;
	};
	
	public function loadNextFile():Void {
		var sNextFile:String = FactoriumApp.getNextFile();
		if(sNextFile){
			mclExternal.loadClip(sNextFile, FactoriumApp.getNextLevel());
		};
		return;
	};
	
	public function loadContent(section:Number, subSection:Number):Void {
		this.mcBeeKy.setEnabled(false);
		this.mcPaisaje.zoomOut();
		this.mcShadow.shadowIn();
		this.mcControl.setEnabledButtons(true);
		this.mcControl.controlIn();
				
		switch(section){
			case FactoriumApp.OBJETIVO:
			this.setActiveContent(this.getObjetivoContent());
			break;
			
			case FactoriumApp.MECANICA:
			this.setActiveContent(this.getMecanicaContent());
			break;
			
			case FactoriumApp.INFO:
			trace("FactoriumApp.as | loadContent() | todavía no existe la info");
			break;
			
			default:
			throw new Error("FactoriumApp.as | loadContent() | sección inexistente");
			break;
		};

		this.getContent().showContent();
		return;
	};
	
	public function unloadContent():Void {
		//trace("FactoriumApp.as | unloadContent()");
		this.mcControl.controlOut();
		this.mcShadow.shadowOut();
		this.mcBeeKy.setEnabled(true);
		this.mcPaisaje.zoomIn();
		//this["mcPaisaje"]["zoomIn"]();
		
		return;
	};
	
	public function getContent():FactoriumContent {
		return this.activeContent;
	};

	public function getObjetivoContent():FactoriumContent {
		return this.objetivoContent;
	};
	public function setObjetivoContent(c:FactoriumContent):Void {
		this.objetivoContent = c;
	};
	public function getMecanicaContent():FactoriumContent {
		return this.mecanicaContent;
	};
	public function setMecanicaContent(c:FactoriumContent):Void {
		this.mecanicaContent = c;
	};

	
}// end class